
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;

public class FrameDirectoryChooser extends javax.swing.JFrame 
{
    private FrameStart start_frame;
    public FrameDirectoryChooser(FrameStart start_frame) 
    {
        this.start_frame = start_frame;
        initComponents();
    }
    public void initFrame()
    {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize(); 
        Dimension framesize = this.getSize();
        int x = (int) ( screenSize.getWidth() - framesize.getWidth() ) / 2;
        int y = (int) ( screenSize.getHeight() - framesize.getHeight() ) / 2;
        setLocation( x, y );
        setResizable(false);
        setVisible( true );
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        directory_chooser = new javax.swing.JFileChooser();
        button_choose_directory = new javax.swing.JButton();
        button_cancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        directory_chooser.setControlButtonsAreShown(false);
        directory_chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_AND_DIRECTORIES);

        button_choose_directory.setText("Choose directory");
        button_choose_directory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_choose_directoryActionPerformed(evt);
            }
        });

        button_cancel.setText("Cancel");
        button_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_cancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(directory_chooser, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
                .addGap(18, 18, 18))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(94, Short.MAX_VALUE)
                .addComponent(button_choose_directory)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(button_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(101, 101, 101))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(directory_chooser, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(button_choose_directory, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                    .addComponent(button_cancel, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void button_choose_directoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_choose_directoryActionPerformed
    File f = directory_chooser.getSelectedFile();
    if(f.isDirectory())
        start_frame.getDirectoryTextField().setText(f.getAbsolutePath());    
    if(f.isFile())
        System.out.print("Selected file is: " + f.getAbsolutePath() + "\n You must choose directory");
    closeFrame();
}//GEN-LAST:event_button_choose_directoryActionPerformed

private void button_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_cancelActionPerformed
    closeFrame();   
}//GEN-LAST:event_button_cancelActionPerformed
public void closeFrame()
{
    setVisible(false);
}
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new FrameDirectoryChooser().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button_cancel;
    private javax.swing.JButton button_choose_directory;
    private javax.swing.JFileChooser directory_chooser;
    // End of variables declaration//GEN-END:variables

}
